﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.goods
{
    public partial class scale_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            //取到操作类型
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.sf_manager_scale().Exists(this.id))
                {
                    JscriptMsg("级别不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                RoleBind(ddlTypeId);
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 比例级别=================================
        private void RoleBind(DropDownList ddl)
        {
            BLL.sf_manager_scale scaleBll = new BLL.sf_manager_scale();

            Model.manager mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
            string busnieseNum = mgr.busnieseNum.ToString();
            string appNum = Session[DTKeys.SESSION_APP_INFO].ToString();
            string templatesNum = Session[DTKeys.SESSION_TEMPLATES_INFO].ToString();
            string appWhere = Utils.getWhereByInfo(busnieseNum, appNum,templatesNum);
            DataTable dt = scaleBll.GetList(appWhere + " order by id asc").Tables[0];

            ddl.Items.Clear();
            ddl.Items.Add(new ListItem("请选择级别...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                ddl.Items.Add(new ListItem(dr["levelName"].ToString(), dr["levels"].ToString()));
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.sf_manager_scale scaleBll = new BLL.sf_manager_scale();
            Model.sf_manager_scale model = scaleBll.GetModel(_id);
            ddlTypeId.SelectedValue = model.levels.ToString();
            txtColorName.Text = (model.scale*100).ToString();
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.sf_manager_scale scaleBll = new BLL.sf_manager_scale();
            Model.sf_manager_scale model = scaleBll.GetModel(_id);

            model.scale = Convert.ToDecimal(txtColorName.Text.Trim())/100;
            model.levels = Convert.ToInt32(ddlTypeId.SelectedValue);
            if (scaleBll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改比例:" + model.levelName); //记录日志
                result = true;
            }

            return result;
        }
        #endregion


        // 提交保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改比例成功！", "scale_list.aspx");
            }
        }

    }
}